package com.benjaminwan.read.free.ui.compatibility

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHidDevice
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.base.fragment.BaseFragment
import com.benjaminwan.read.free.common.singleTopNavOptions
import com.benjaminwan.read.free.databinding.FragmentCompatibilityBinding
import com.benjaminwan.read.free.ui.MainActivity

class CompatFragment : BaseFragment() {
    private val compatVM by fragmentViewModel(CompatViewModel::class)

    private val btAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

    private var bluetoothProfile: BluetoothProfile? = null

    private val toolbar: Toolbar
        get() = (requireActivity() as MainActivity).binding.toolbar

    private val mProfileListener = object : BluetoothProfile.ServiceListener {
        override fun onServiceConnected(profileType: Int, bluetoothProfile: BluetoothProfile?) {
            this@CompatFragment.bluetoothProfile = bluetoothProfile
            if (profileType == BluetoothProfile.HID_DEVICE) {
                compatVM.setCompat(true)
                /*lifecycleScope.launchWhenResumed {
                    findNavController().navigate(R.id.loginFragment, null, singleTopNavOptions)
                }*/
            }
        }

        override fun onServiceDisconnected(p0: Int) {

        }

    }

    private val mProfileReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(
            param1Context: Context, param1Intent: Intent
        ) {

        }
    }

    private lateinit var binding: FragmentCompatibilityBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentCompatibilityBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        btAdapter?.getProfileProxy(requireContext(), mProfileListener, BluetoothProfile.HID_DEVICE)
        val intentFilter = IntentFilter()
        intentFilter.addAction(BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED)
        requireActivity().registerReceiver(this.mProfileReceiver, intentFilter)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        toolbar.visibility = View.GONE
        binding.ignoreErrBtn.setOnClickListener {
            //findNavController().navigate(R.id.loginFragment, null, singleTopNavOptions)
            findNavController().navigate(R.id.guideFragment, null, singleTopNavOptions)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        toolbar.visibility = View.VISIBLE
    }

    override fun invalidate() = withState(compatVM) { compat ->
        if (compat.isCompat) {
            binding.msgTv.setText(R.string.compatibility_result_ok)
        } else {
            binding.msgTv.setText(R.string.compatibility_result_failed)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        btAdapter?.closeProfileProxy(
            BluetoothProfile.HID_DEVICE, bluetoothProfile
        )
        requireActivity().unregisterReceiver(mProfileReceiver)
    }
}